package com.example.administrator.yunniuusedcar.ui;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.administrator.common.base.BaseActivity;
import com.example.administrator.yunniuusedcar.MainActivity;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.fragment.AgeFragment;
import com.example.administrator.yunniuusedcar.fragment.DefaultFrament;
import com.example.administrator.yunniuusedcar.fragment.KilometreNumberFrament;
import com.example.administrator.yunniuusedcar.fragment.MainChooseCarFragment3Model;
import com.example.administrator.yunniuusedcar.fragment.MineFragment5Model;
import com.example.administrator.yunniuusedcar.fragment.PingJingDefaultFragment;
import com.example.administrator.yunniuusedcar.fragment.PingJingXiaoLiangFragment;
import com.example.administrator.yunniuusedcar.fragment.PingJing_JiaGeFragment;
import com.example.administrator.yunniuusedcar.fragment.PriceFragment;
import com.example.administrator.yunniuusedcar.fragment.ScreenFragment;

/**  平行进口
 * Created by USER2015 on 2017/12/23.
 */

public class ShopDetailActivity extends BaseActivity {

    Fragment[] fragments ;       //activity_secon_hand_car
    private RadioGroup mRgBottomMenu ;
    //当前fragment的下标
    private int currentIndex ;
    private TextView tv_titleName;
    private ImageView iv_bacds;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_posion1;
    }

    @Override
    public void initView() {
        mRgBottomMenu  = findView(R.id.rg_bottom_menu_ZG);    // RadioGroup
        tv_titleName = findView(R.id.tv_ESHcar);
        tv_titleName.setText("平行进口");
        iv_bacds = findView(R.id.iv_back);
        iv_bacds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initFg();
    }

    private void initFg() {
        fragments = new Fragment[]{   // 讲fragment加入数组
                // 综合排序，销量优先，价格排序，筛选
                new PingJingDefaultFragment(), new PingJingXiaoLiangFragment(),   //
                new PingJing_JiaGeFragment(), new ScreenFragment()
        };
        // 开启事物
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //设置为默认界面 MainHomeFragment
        ft.add(R.id.main_content,fragments[0]).commit();

        //RadioGroup选中事件监听 改变fragment  RadioGroup
        mRgBottomMenu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_default_ZG :  // 综合
                        setIndexSelected(0);
                        break;
                    case R.id.rb_KLnum_ZG:   // 销量
                        setIndexSelected(1);
                        break;
                    case R.id.rb_age_ZG :  // 价格
                        setIndexSelected(2);
                        break;
                    case R.id.rb_person_ZG :  // 筛选
                        setIndexSelected(3);
                    Intent intent = new Intent(ShopDetailActivity.this,MainActivity.class);
                        Intent intent1;
                        intent.putExtra("fragid",3);
                        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.view_pager , new MainChooseCarFragment3Model());  // 选车
                        startActivity(intent);

                        break;
                }
            }
        });
    }

    // 设置fragment 界面
    private void setIndexSelected(int index) {
        if (currentIndex == index){
            return;
        }
        //开启事务
        FragmentTransaction ft1 = getSupportFragmentManager().beginTransaction();
        //隐藏当前Fragment
        ft1.hide(fragments[currentIndex]);
        // 判断当前的fragmetn是否已添加
        if (!fragments[index].isAdded()){
            ft1.add(R.id.main_content,fragments[index]).show(fragments[index]);
//            ft1.add(R.id.main_content,fragments[index].show(fragments[index]));
        }else {
            //否则显示新 fragment
            ft1.show(fragments[index]);

        }ft1.commit();
        currentIndex = index ;

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }
}
